package cfca.seal.web.tag;

import cfca.seal.system.Env;
import cfca.seal.util.IdentificationTypeUtil;
import cfca.seal.util.StringUtil;
import cfca.seal.web.tag.base.TemplateDirectiveModelBase;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class IdentificationTypeTag extends TemplateDirectiveModelBase
{
  public String process(Environment env, Map paramsMap, TemplateModel[] loopVars, TemplateDirectiveBody body)
    throws TemplateException, IOException
  {
    StringBuffer sf = new StringBuffer();
    sf.append("<select size=\"1\" class=\"").append(this.className).append("\" id=\"").append(this.elementId).append("\" name = \"").append(this.elementName).append("\" >");

    sf.append("<option value=\"\" ");
    sf.append(" >").append("-- 请选择 --").append("</option>");

    log.info("加载证件类型信息配置文件：identificationType.properties");
    IdentificationTypeUtil identificationType = new IdentificationTypeUtil(Env.configDir + File.separator + "identificationType.properties");

    Enumeration en = identificationType.getPropertyNames();
    while (en.hasMoreElements()) {
      String key = (String)en.nextElement();
      String value = identificationType.getPropertyValueByKey(key);
      sf.append("<option value=\"").append(key).append("\" ");
      if ((StringUtil.isNotEmpty(this.elementValue)) && (key.equals(this.elementValue)))
        sf.append(" selected=\"selected\" ");
      sf.append(" >").append(value).append("</option>");
    }

    sf.append("</select>");
    return sf.toString();
  }
}